import fs from 'fs';
import querystring from 'querystring'
import url from 'url'
import path from 'path'
function shop(req,res){

    let {user}=querystring.parse(url.parse(req.url).query)
        user=JSON.parse(user)
    let filePath=path.join(__dirname,'../data/list.json')
    let userlist=fs.readFileSync(filePath)
    // let userlist=fs.writeFileSync('./data/list.json',JSON.stringify(list),'utf-8')
     //判断文件里是否为空 
     if(userlist.length===0){
        //创建一个数组 push数据 转成json放进文件中
        let brr=[];
        brr.push(user)
        console.log(brr)
        fs.writeFileSync('./data/list.json',JSON.stringify(brr),'utf-8')
        
    }else{
        userlist=JSON.parse(userlist);
        console.log(userlist)
        let msg =userlist.some(item=>{
                return item.name===user.name
        })
        console.log(msg)
        if(msg){
            res.send({code:1})
        }else{
            userlist.push(user)
            fs.writeFileSync('./data/list.json',JSON.stringify(userlist),'utf-8')
            res.send({code:2})
        }
    }
}
export {shop}